package org.testframe;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 流程池
 * @author 丁常磊
 * @date 2023/11/17 9:51
 */

public class FlowPool {

    private final List<Flow> flows = new ArrayList<>();

    /**
     * 取出一个流程
     * 如果内部没有可执行的流程则结束
     * @return
     */
    public synchronized Flow getFlow() {

        if (flows.size() == 0) {
            return null;
        }

        Random random = new Random();
        return flows.remove(random.nextInt(flows.size()));
    }

    /**
     * 添加一个流程
     * @param flow
     */
    public synchronized void addFlow(Flow flow) {
        flows.add(flow);
    }

}
